package com.test.config;


import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j2;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Date;

@Log4j2
public class DatePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        log.info("collection:" + JSON.toJSONString(collection) + ",preciseShardingValue:" + JSON.toJSONString(preciseShardingValue));
        Date date = preciseShardingValue.getValue();
        LocalDateTime dateTime =LocalDateTime.ofEpochSecond(date.getTime()/1000,0, ZoneOffset.ofHours(8));

        Integer year = dateTime.getYear();
        Integer month = dateTime.getMonth().getValue();
        if (month > 6) {
            return "t_order_" + year + "_2";
        } else {
            return "t_order_" + year + "_1";
        }

    }
}
